package com.yushu.base.controller;

import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.yushu.base.pojo.vo.DicSetVO;
import com.yushu.base.pojo.bo.DicSet;
import com.yushu.base.service.DicSetService;
import com.yushu.common.execption.CgException;
import com.yushu.common.anno.LogAop;
import com.yushu.common.shiro.ShiroUtil;
import com.yushu.common.util.pager.Pages;
import com.yushu.common.util.result.Result;

import org.apache.commons.lang3.StringUtils;

//import io.swagger.annotations.Api;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author test
 * @CreateDate 2020-12-03 15:47:25
 */
//@Api(description = "字典集管理")
@Controller
@RequestMapping("/base/dicSet")
public class DicSetController {
	private Logger logger = Logger.getLogger(DicSetController.class);

    @Autowired
    private DicSetService dicSetService;
    private Map  <String, Object> resultMap;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   //true:允许输入空值，false:不能为空值
    }
    /**
    * 分页条件查询字典集数据
    * 2020-03-08 15:46:49
    * @param tablePage
    * @return
    */
    @LogAop("分页查询字典集")
    @ResponseBody
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public Pages page(@RequestBody Pages<DicSetVO> tablePage) {
        Pages page = dicSetService.page(tablePage);
        return page;
    }
	/**
    * 查询全部字典集数据
    * 2020-03-08 15:46:49
    */
    @ResponseBody
    @RequestMapping(value = "/queryList", method = RequestMethod.POST)
    public List<DicSet> queryList(@RequestBody DicSet dicSet) {
        List<DicSet> list = dicSetService.selectList(dicSet);
        return list;
    }
	/**
	* 添加或者更新字典集
	*2020-03-08 15:46:49
	* @param dicSet
	* @return
	*/
	@LogAop("更新或新增字典集")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public  Result  save(@RequestBody DicSet dicSet ) {
        try {
			if (!StringUtils.isEmpty(dicSet.getId())) {
				logger.info("进入修改字典集方法");
				dicSetService.updateByKey(dicSet);
                return Result.success("字典集修改成功");
			} else {
				logger.info("进入添加字典集方法");
				dicSetService.insert(dicSet);
                return Result.success("字典集新增成功");
			}
	        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());

        }       
	}
	
    /**
    *删除字典集数据
    *2020-03-08 15:46:49
    * @param dicSet 需要删除的对象
    */
    @LogAop("删除字典集")
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result delete(Model model,  @RequestBody DicSet dicSet ) {
        try {
            dicSetService.deleteById(dicSet.getId());
	        logger.info("删除字典集成功");
	        return Result.success("删除字典集成功");
        } catch (CgException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
    *
    * 批量删除字典集数据
    * 2020-03-08 15:46:49
    * @param dicSets 字典集集合
    */
    @LogAop("批量删除字典集")
    @RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
    @ResponseBody
    public Result batchDelete(Model model, @RequestBody List<DicSet> dicSets) {
        logger.info("进入到删除字典集数据方法");
        try {
            dicSetService.deleteBatch(dicSets);
            return Result.success("批量删除字典集成功");
        } catch (CgException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());

        }
    }

    /**
    * 跳转字典集列表页面
    * 2020-03-08 15:46:49
    */
    @RequestMapping(value = "toDicSetList", method = RequestMethod.GET)
    public String toDicSetList(Model model ) {
        model.addAttribute("title","字典集管理");
        model.addAttribute("page","base/dicSet/list");
        return "menuPage";
    }

    /**
    * 跳转字典集跳转到空白列表页面
    * 2020-03-08 15:46:49
    */
    @RequestMapping(value = "toOpenDicSetList", method = RequestMethod.GET)
    public String toOpenDicSetList(Model model,HttpServletRequest request) {
        model.addAttribute("title", "字典集管理");
        model.addAttribute("page", "base/dicSet/list");
        return "openPage";
    }

    /**
    * 跳转字典集新增或修改页面
    * 2020-03-08 15:46:49
    */
    @RequestMapping(value = "toEditor", method = RequestMethod.GET)
    public String toEditor(Model model, String id , HttpServletRequest request) {
        if(StringUtils.isNotEmpty(id)){
	        request.setAttribute("data",  dicSetService.selectById(id));
        }
        model.addAttribute("title", "字典集管理");
        model.addAttribute("page", "base/dicSet/addUpdate");
        return "openPage";
    }
    @RequestMapping("checkRepeat")
    @ResponseBody
    public String checkRepeat(@RequestBody Map map ) {
        return dicSetService.checkRepeat(map);
    }    
    

    /**
     * 跳转新增或修改页面
     * 2019-08-28 16:03:04
     */
    @ResponseBody
    @RequestMapping(value = "/selectDicSet", method = RequestMethod.POST)
    public List<DicSet> selectDicSet(@RequestBody Map dicIdMap) {
        String dicId = dicIdMap.get("dicId")+"";
        return dicSetService.selectDicSetList(dicId);
    }
}

